﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Xylee.Common.Modules
{
    /// <summary>
    /// Runs as a loader and unloading for running, 
    /// installing, and uninstalling plugin modules 
    /// for the application. Use this for boot strap 
    /// loading to a particular application.
    /// </summary>
    public class BootStrapLoader
    {
        public System.Reflection.Assembly DownloadModule(string moduleUri)
        {
            //if (Application.Current..Contains(myLocation))
            //{
            //    string assemblyLocation = (string)Application.Current.Properties[myLocation];
            //    FileStream stream = new FileStream(assemblyLocation, FileMode.Open);
            //    BinaryReader reader = new BinaryReader(stream);
            //    byte[] assembly = reader.ReadBytes((int)stream.Length);
            //    reader.Close();
            //    stream.Close();
            //    // Assembly already exists, load that instead
            //    LoadedNotify(assembly);
            //    return;
            //}
            return null;
        }

        public void LoadModuleFromFile(string moduleUri, string fileUri)
        {
            System.Reflection.Assembly module = System.Reflection.Assembly.LoadFile(fileUri);
            LoadModule(module, moduleUri);
        }

        /// <summary>
        /// Loads a particular module in the library by 
        /// adding the installed module into the installation 
        /// directory. This module must not exist within the available
        /// modules of the Xyliea Application instance.
        /// </summary>
        /// <param name="module">The <see cref="System.Reflection.Assembly"/>
        /// to load into the application.</param>
        public void LoadModule(System.Reflection.Assembly module, string moduleUri)
        {
            ModuleLoad(module, moduleUri, true);
        }

        /// <summary>
        /// Unloads a particular module from the library by 
        /// removing the installed module from the installation 
        /// directory. This module must exist within the available
        /// modules of the Xyliea Application instance.
        /// </summary>
        /// <param name="module">The <see cref="System.Reflection.Assembly"/>
        /// to unload from the application.</param>
        public void UnloadModule(System.Reflection.Assembly module, string moduleUri)
        {
            ModuleLoad(module, moduleUri, false);
        }

        private void ModuleLoad(System.Reflection.Assembly module, string moduleUri, bool load)
        {
            Module xyModule = new Module(module.GetName().Name, module.GetName().Version.ToString(), moduleUri);
            foreach (Type t in module.GetTypes())
            {
                bool isModule = false;
                foreach (Type interfaceType in t.GetInterfaces())
                {
                    if (interfaceType == typeof(Xylee.Common.IPortal))
                    {
                        isModule = true;
                        break;
                    }
                }

                if (isModule)
                {
                    if (load)
                        xyModule.AddPortal(t.AssemblyQualifiedName);
                }
            }
            XApp.Current.AvailableModules.Add(xyModule);
        }
    }
}
